package ep.model;

public class Ant {

	private int i;

	private int j;

	private Direction direction;

	public Ant(int i, int j) {
		super();
		this.i = i+1;
		this.j = j+1;
		this.direction = Direction.NORTH;
	}

	public static Ant parse(String string){
		String[] pos = string.split("x");
		return new Ant(Integer.parseInt(pos[0]), Integer.parseInt(pos[1]));
	}
	
	public int getI() {
		return i;
	}

	public int getJ() {
		return j;
	}

	public void setI(int i) {
		this.i = i;
	}

	public void setJ(int j) {
		this.j = j;
	}

	public void turnCW() {
		this.direction = this.direction.getDirectionCW();
	}

	public void turnCCW() {
		this.direction = this.direction.getDirectionCCW();
	}

	public void doOneStep() {
		if (this.direction.equals(Direction.NORTH)) {
			this.i--;
		} else if (this.direction.equals(Direction.EAST)) {
			this.j++;
		} else if (this.direction.equals(Direction.SOUTH)) {
			this.i++;
		} else {
			this.j--;
		}

		// Uma alternativa para evitar comparacoes seria fazer assim:
		// int ordinal = this.direction.ordinal();
		// this.i = i + ((ordinal + 1) % 2) * (ordinal - 1);
		// this.j = j + (ordinal % 2) * (ordinal - 2);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((direction == null) ? 0 : direction.hashCode());
		result = prime * result + i;
		result = prime * result + j;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Ant other = (Ant) obj;
		if (direction == null) {
			if (other.direction != null)
				return false;
		} else if (!direction.equals(other.direction))
			return false;
		if (i != other.i)
			return false;
		if (j != other.j)
			return false;
		return true;
	}
	
	
	
}
